package com.santoker.user.service.service.biz.auth.impl;

import com.santoker.baseservice.ApiResponseBody;
import com.santoker.baseservice.ApiResponseCode;
import com.santoker.baseservice.redis.AuthInfo;
import com.santoker.baseservice.util.DateUtils;
import com.santoker.user.service.dto.vo.auth.LoginResponseVo;
import com.santoker.user.service.dto.vo.auth.LoginVo;
import com.santoker.user.service.dto.vo.user.UserInfoModifyVo;
import com.santoker.user.service.exception.CommonServiceException;
import com.santoker.user.service.model.AdminUser;
import com.santoker.user.service.model.User;
import com.santoker.user.service.service.biz.auth.AuthAdminService;
import com.santoker.user.service.service.common.IAdminUserService;
import com.santoker.user.service.service.common.IUserService;
import com.santoker.user.service.service.common.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
public class AuthAdminServiceImpl extends AuthBaseServiceImpl implements AuthAdminService {

    @Autowired
    private IAdminUserService iAdminUserService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private IUserService iUserService;

    @Override
    public LoginResponseVo accountLogin(LoginVo body) {

        log.info("------------------------------ 账号登录  ------------------------------");
        ApiResponseBody.printRequestParam(body);

        // 验证手机号是否存在
        AdminUser adminUser = iAdminUserService.getUserByAccount(body.getAccount());
        if (Objects.isNull(adminUser)) {
            throw new CommonServiceException(ApiResponseCode.USER_NOT_EXIST);
        }

        // 校验密码是否匹配
        if (!verifyPassword(adminUser.getPassword(), body.getPassword())) {
            throw new CommonServiceException(ApiResponseCode.PASSWORD_WRONG);
        }

        // 获取登录信息
        LoginResponseVo loginResponseVo = getLoginAdminResponse(adminUser, body.getSid(), body.getCid());
        return loginResponseVo;
    }

    @Override
    public void logout(AuthInfo authInfo) {

        log.info("------------------------------ 登出  ------------------------------");
        ApiResponseBody.printRequestParam(authInfo);

        redisService.removeToken(authInfo);
    }

    @Override
    public void updateUserInfo(UserInfoModifyVo body) {

        log.info("------------------------------ 编辑用户信息  ------------------------------");
        ApiResponseBody.printRequestParam(body);

        // 验证用户是否存在
        User user = iUserService.getById(body.getId());
        if (Objects.isNull(user)) {
            throw new CommonServiceException(ApiResponseCode.USER_NOT_EXIST);
        }

        // 修改用户昵称：新昵称不为空，且新昵称与旧昵称不一致时更新
        if (StringUtils.isNotBlank(body.getNickname()) && !body.getNickname().equals(user.getNickname())) {
            user.setNickname(body.getNickname());
        }

        // 修改用户手机号
        if (StringUtils.isNotBlank(body.getMobile()) && !body.getMobile().equals(user.getMobile())) {
            // 验证手机号是否存在
            User user2 = iUserService.getUserByMobile(body.getMobile());
            if (Objects.nonNull(user2)) {
                throw new CommonServiceException(ApiResponseCode.REGISTER_DUPLICATED_PHONE);
            }
            user.setMobile(body.getMobile());
        }

        // 更新用户信息
        user.setUpdatedAt(DateUtils.getSecondTimestamp());
        if (!iUserService.updateById(user)) {
            throw new CommonServiceException(ApiResponseCode.USER_UPDATE_FAILURE);
        }
    }

}
